package com.example.xmltohtml.html;

import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlSaveOptions;
import com.aspose.pdf.License;
import com.aspose.pdf.SaveOptions;

import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class PdfToHtml2 {
    private static final AtomicBoolean HAS_LICENSED = new AtomicBoolean();

    public static void main(String[] args) throws Exception {
        PdfToHtml2 pdfToHtml = new PdfToHtml2();
        pdfToHtml.handle(new BufferedInputStream(
                new FileInputStream("M:\\java\\pdftohtml\\H2_AN202505081669303055_1.pdf")),
                new FileOutputStream("M:\\java\\pdftohtml\\H2_AN202505081669303055_111.html"));
    }

    private static final HtmlSaveOptions HTML_SAVE_OPTIONS = new HtmlSaveOptions();
    static {
        HTML_SAVE_OPTIONS.setFixedLayout(true);
        HTML_SAVE_OPTIONS.setPageBorderIfAny(new SaveOptions.BorderInfo());
        HTML_SAVE_OPTIONS.setPartsEmbeddingMode(HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml);
        HTML_SAVE_OPTIONS.setRasterImagesSavingMode(
                HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground
        );
    }


    public void handle(BufferedInputStream inputStream, OutputStream outputStream) throws Exception {
        Licenses.tryLoadLicense(HAS_LICENSED, new License()::setLicense);
        postHandle(new Document(inputStream), outputStream);
    }

    private void postHandle(Document document, OutputStream outputStream) {
        HtmlTransfer.create(os -> document.save(os, HTML_SAVE_OPTIONS)).transfer(outputStream);
    }

}
